package org.carrot2.examples.clustering;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.carrot2.core.Document;

import weka.core.Stopwords;

public class hashClustering {

	public HashMap<String,ArrayList<Document>> clustersList;
	public HashMap<String,String> thumbUrl;
	public static  ArrayList<String> stopwords;
	public hashClustering(HashMap<String,String> param1){
		clustersList=new HashMap<String,ArrayList<Document>>();
		thumbUrl=param1;
		stopwords=new ArrayList<String>();
		stopwords.add("-");
		stopwords.add(":");
		stopwords.add(",");
		stopwords.add("'");
	}
	public String cluster(ArrayList<Document> dList){
		
		
		
		
		for (Document d:dList){
			
			String title=d.getTitle();			
			title=removePunctuation(title);
			
			StringTokenizer t1= new StringTokenizer(title);
			StringBuffer buff=new StringBuffer();
			while(t1.hasMoreTokens()){
				String t2=t1.nextToken();
				if(!Stopwords.isStopword(t2) && stopwords.indexOf(t2)<0){
					buff.append(t2+" ");
				}
			}
			
			String t3=buff.toString().trim();
			
			
			
			
			if(!clustersList.containsKey(t3)){
				
				clustersList.put(t3, new ArrayList<Document>());
			}
			
			clustersList.get(t3).add(d);
			
		}
		
		
		
		
		return rankAndDisplay().toString();
		
	}
	
	
	
	public String removePunctuation(String title){
		
		for (String abc: stopwords){
			title=title.replace(abc, " ");			
		}
		
		return title;
	}
	
	public String rankAndDisplay(){
		
		
		StringBuffer sb=new StringBuffer();
		List<core.cluster> listClusters = new ArrayList<core.cluster>();
		
		for(String key:clustersList.keySet()){
			
			core.cluster temp=new core.cluster();
			temp.setScore(clustersList.get(key).size());
			temp.setLabel(key);			
			listClusters.add(temp);
			
		}
		
		Comparator<core.cluster> clusterComparator = new Comparator<core.cluster>() {
	        @Override public int compare(core.cluster c1, core.cluster c2) {
	        	if(c1.getScore()>c2.getScore())
	        		return 0;
	        	else
	        		return 1;
	            
	        }           
	    };
		Collections.sort(listClusters,clusterComparator);
		int clusterShown=0;
		
		
		
		for(core.cluster a:listClusters){
			
			if(clusterShown++>10)break;
			sb.append("<div style='background-color: white;border-radius: 10px 10px 10px 10px;box-shadow: 3px 2px 7px black; padding:20px ;overflow-x:auto;top:4px;margin-top:10px' ><center>");
			sb.append("<div id='head' style='background-color: grey;border-radius: 10px 10px 10px 10px;box-shadow: 3px 2px 7px black;' ><center>");
			sb.append("<a href='http://stage2.images.search.yahoo.com/search/images?p="+a.getLabel()+"&ei=utf-8&y=Search&imgs=yahoohq' target='blank' >"+a.getLabel()+"</a>");
			sb.append("</div>");
			sb.append("<table><tr>");
			//sb.append("<tr><td colspan='3'><a href='http://stage2.images.search.yahoo.com/search/images?p="+a.getLabel()+"&ei=utf-8&y=Search&imgs=yahoohq' target='blank'>"+a.getLabel()+"</td></tr>");
			

			
			ArrayList<Document> list=clustersList.get(a.getLabel());
			int maxDocsShown=0;
			
			
			for(Document d: list){
				
				
				if(maxDocsShown++>10)break;
				sb.append(displayDocument(d,clusterShown));
				
			}
			
			sb.append("</tr>");
			sb.append("</table>");
			
			sb.append("</center></div>");
		}
		
		
		return sb.toString();
		
		
	}
	
	
	public String displayDocument( Document document,int limit)
    {

		final String url = document.getField(Document.CONTENT_URL);
        StringBuffer sb=new StringBuffer();
        if (StringUtils.isNotBlank(url))
        {
        	String var=thumbUrl.get(url);
        	
        	var=var.replaceAll("http://pix.zenfs.com", "http://d4.yimg.com/sr");
        	sb.append("<td><table><tr><td><a class='fancybox' rel='"+limit+"' href='"+var+"'title='"+document.getField(Document.TITLE)+"'><img  src=\""+url+"\" style='box-shadow: 10px 10px 15px grey;'/></a></td></tr><tr><td><strong>"+document.getField(Document.TITLE)+"</strong></td></tr><tr><td>"+document.getField(Document.SUMMARY)+"</td></tr></table></td>");

        }
        return sb.toString();
        

        
    }

	
}
